package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQueryScenePersonInfoService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OperatorCustInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.AssemblyAttrDtoUtils;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.util.I18nUtil;
import com.ztesoft.bss.svcability.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <Description> 经办人-用户停机 <br>
 *
 * @author <br>
 *
 */
@Service
public class OperatorCustInfoModuleHandler extends AbstractModuleHandler {


    @Autowired
    private ILocalQueryScenePersonInfoService queryScenePersonService;


    @Override
    public List<ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        List<InstAttrDTO> attrConfigs = queryScenePersonService.listHandlerCustInfoAttrs(context.getAcceptRequest().getSceneInstId())
                .getResultObject().stream().filter(instAttrDTO -> LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D.equals(instAttrDTO.getShowType())).collect(Collectors.toList());
        List<CommonAttrVO> commonAttrList =  AssemblyAttrDtoUtils.assemblyVO(attrConfigs);

        // 去掉下拉框值 "请选择" 的空选项
        if (!ListUtil.isEmpty(commonAttrList)) {
            for (CommonAttrVO commonAttr: commonAttrList) {
                if (!KeyConsts.DROPDOWN.equals(commonAttr.getAttrValueTypeId())) {
                    continue;
                }
                List<StaticAttrDO> attrValues = commonAttr.getAttrValues();
                if (KiteListUtils.isEmpty(attrValues)) {
                    continue;
                }
                Iterator<StaticAttrDO> iter = attrValues.iterator();
                while (iter.hasNext()) {
                    StaticAttrDO attrValue = iter.next();
                    if (I18nUtil.getMessage("BSS_KITE_ATTR_43033").equals(attrValue.getAttrValueDesc())) {
                        iter.remove();
                    }
                }
            }
        }

        OperatorCustInfoVO operatorCustInfoVO = new OperatorCustInfoVO();
        operatorCustInfoVO.setOperatorCustAttr(commonAttrList);

        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setOperatorCustInfo(operatorCustInfoVO);

        return Collections.singletonList(operatorCustInfoVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        List<InstAttrDTO> attrConfigs = queryScenePersonService.listHandlerCustInfoAttrs(context.getAcceptRequest().getSceneInstId())
            .getResultObject();

        OperatorCustInfoVO operatorCustInfoVO = new OperatorCustInfoVO();
        operatorCustInfoVO.setOperatorCustAttr(AssemblyAttrDtoUtils.assemblyCommonVO(attrConfigs));

        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setOperatorCustInfo(operatorCustInfoVO);

        return Collections.singletonList(operatorCustInfoVO);
    }

}
